package com.hanlink.dsp.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;


import com.edge.pbxdsp.R;

import org.xclcharts.chart.AreaChart;
import org.xclcharts.chart.AreaData;
import org.xclcharts.chart.CustomLineData;
import org.xclcharts.common.DensityUtil;
import org.xclcharts.renderer.XEnum;
import org.xclcharts.view.ChartView;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by PeterVon on 2015-11-18.
 */
public class EqChartView extends ChartView {

    private String TAG = "EqChartView";
    private AreaChart chart = new AreaChart();
    //标签集合
    private LinkedList<String> mLabels = new LinkedList<String>();
    //数据集合
    private LinkedList<AreaData> mDataset = new LinkedList<AreaData>();

    private List<CustomLineData> mCustomLineDataset = new LinkedList<CustomLineData>();

    static List<Double> defaultValue = new ArrayList<Double>();

    static {
        for (int i = 0; i < 31; i++) {
            defaultValue.add(0d);
        }
    }

    private AreaData mAreaData = new AreaData("", defaultValue,
            Color.rgb(182, 23, 123),
            Color.rgb(255, 191, 235)
    );

    public EqChartView(Context context) {
        super(context);
        initView();
    }

    public EqChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public EqChartView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }


    //Demo中bar chart所使用的默认偏移值。
    //偏移出来的空间用于显示tick,axistitle....
    protected int[] getBarLnDefaultSpadding() {
        int[] ltrb = new int[4];
        ltrb[0] = DensityUtil.dip2px(getContext(), 0); //left
        ltrb[1] = DensityUtil.dip2px(getContext(), 0); //top
        ltrb[2] = DensityUtil.dip2px(getContext(), 0); //right
        ltrb[3] = DensityUtil.dip2px(getContext(), 0); //bottom
        return ltrb;
    }


    private void initView() {
        chartLabels();
        chartDataSet();
        chartRender();
    }


    public void setAreaData(List<Double> areaData) {
        mAreaData.setDataSet(areaData);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //图所占范围大小
        chart.setChartRange(w, h);
    }

    private void chartRender() {
        try {
            //设置绘图区默认缩进px值,留置空间显示Axis,Axistitle....
            int[] ltrb = getBarLnDefaultSpadding();
            chart.setPadding(ltrb[0], ltrb[1], ltrb[2], ltrb[3]);

            //轴数据源
            //标签轴
            chart.setCategories(mLabels);
            //数据轴
            chart.setDataSource(mDataset);

            //禁止平移
            chart.disablePanMode();

            //数据轴最大值
            chart.getDataAxis().setAxisMax(25);
            chart.getDataAxis().setAxisMin(-25);
            //数据轴刻度间隔
            chart.getDataAxis().setAxisSteps(1);

            //网格
/*            chart.getPlotGrid().showHorizontalLines();
            chart.getPlotGrid().showVerticalLines();*/
            //把顶轴和右轴隐藏
            //chart.hideTopAxis();
            //chart.hideRightAxis();
            //把轴线和刻度线给隐藏起来
            chart.getDataAxis().hideAxisLine();
            chart.getDataAxis().hideTickMarks();
            chart.getCategoryAxis().hideAxisLine();
            chart.getCategoryAxis().hideTickMarks();
            //轴标题

            //透明度
            chart.setAreaAlpha(180);

            CustomLineData line1 = new CustomLineData("", 0d, Color.WHITE, 1);
            line1.setCustomLineCap(XEnum.DotStyle.CROSS);
            mCustomLineDataset.add(line1);
            chart.setCustomLines(mCustomLineDataset);

        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, e.toString());
        }
    }

    private void chartDataSet() {
        //将标签与对应的数据集分别绑定
        //标签对应的数据集

        //设置线上每点对应标签的颜色
        mAreaData.getDotLabelPaint().setColor(Color.rgb(0xFF, 148, 235));
        //设置点标签
        //line2.setLabelVisible(true);
        mAreaData.getDotLabelPaint().setTextAlign(Paint.Align.CENTER);
        mAreaData.getDotLabelPaint().setStrokeWidth(1);

        mAreaData.setDotRadius(0);
        mAreaData.setLineColor(Color.RED);
        mAreaData.setApplayGradient(true);
        mAreaData.setGradientDirection(XEnum.Direction.HORIZONTAL);
        mAreaData.setAreaBeginColor(getContext().getResources().getColor(com.hanlink.res.R.color.bg_common));
        mAreaData.setAreaEndColor(getContext().getResources().getColor(com.hanlink.res.R.color.bg_common));

        //mAreaData.setGradientMode(Shader.TileMode.MIRROR);

        mDataset.add(mAreaData);

    }

    private void chartLabels() {
        for (int i = 0; i < 31; i++) {
            mLabels.add("eq" + i);
        }
    }

    @Override
    public void render(Canvas canvas) {
        try {
            chart.render(canvas);
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }

}
